package com.fastloan.jn.mgt.customermanager.level.controller;

import com.fastloan.jn.mgt.all.table.util.GsonUtil;
import com.fastloan.jn.mgt.customermanager.level.model.*;
import com.fastloan.jn.mgt.customermanager.level.model.vo.ManagerLevelExt;
import com.fastloan.jn.mgt.customermanager.level.model.vo.ManagerQuotaSettingExt;
import com.fastloan.jn.mgt.customermanager.level.service.*;
import com.fastloan.jn.utils.other.DozerUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;

import java.lang.reflect.Type;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 客户经理级别Controller
 *
 * @author yanwe
 * createTime 2017-10-2017/10/18 10:38
 */
@RestController
@RequestMapping("/api/web/customerManagerLevel")
public class JnCustomerManagerLevelController extends BaseController<JnCustomerManagerLevel> {

    @Autowired
    private JnCustomerManagerLevelService jnCustomerManagerLevelService;

    @Autowired
    JnManagerLevelParamService managerLevelParamService;

    @Autowired
    JnManagerMonthAssessService managerMonthAssessService;

    @Autowired
    JnManagerPerformanceParamService managerPerformanceParamService;

    @Autowired
    JnManagerQuotaSettingService managerQuotaSettingService;

    @Autowired
    JnManagerRiskService managerRiskService;



    @PostMapping("/pageList")
    public ResultTo pageList() {
        return dataPage();
    }

    @GetMapping("/org/list")
    public ResultTo selectByOrg(){
        return new ResultTo().setData(jnCustomerManagerLevelService.selectByOrg("1"));
    }

    @GetMapping("/{levelId}")
    public ResultTo getLevel(@PathVariable("levelId") String levelId) {
        return new ResultTo().setData(jnCustomerManagerLevelService.selectByPrimaryKey(levelId));
    }

    @PostMapping
    public ResultTo addLevel(JnCustomerManagerLevel jnCustomerManagerLevel){
        jnCustomerManagerLevel.setOrganizationId("1");
        boolean result = jnCustomerManagerLevelService.addCustomerManagerLevel(jnCustomerManagerLevel);
        return result ? new ResultTo() : new ResultTo(ResultEnum.OPERATION_FAILED);
    }

    @PutMapping
    public ResultTo updateLevel(JnCustomerManagerLevel jnCustomerManagerLevel){
        boolean result = jnCustomerManagerLevelService.updateCustomerManagerLevel(jnCustomerManagerLevel);
        return result ? new ResultTo() : new ResultTo(ResultEnum.OPERATION_FAILED);
    }

    @DeleteMapping("/{levelId}")
    public ResultTo deleteLevel(@PathVariable("levelId") String levelId){
        boolean result = jnCustomerManagerLevelService.deleteCustomerManagerLevel(levelId);
        return result ? new ResultTo() : new ResultTo(ResultEnum.OPERATION_FAILED);
    }

    @PostMapping("/levelParam")
    public ResultTo levelParam(){
        List<JnCustomerManagerLevel> customerManagerLevels = jnCustomerManagerLevelService.selectAll();
        List<ManagerLevelExt> managerLevelExts =  customerManagerLevels.stream()
                .map(managerLevel -> DozerUtil.map(managerLevel,ManagerLevelExt.class)).collect(Collectors.toList());
        managerLevelExts.forEach(managerLevelExt -> selectExts(managerLevelExt));
        return new ResultTo().setData(managerLevelExts);
    }


    @PostMapping("/levelQuotaSetting")
    public ResultTo levelQuotaSetting(){
        List<JnCustomerManagerLevel> customerManagerLevels = jnCustomerManagerLevelService.selectAll();
        List<JnManagerQuotaSetting> managerQuotaSettings = managerQuotaSettingService.selectAll();
        List<ManagerQuotaSettingExt> monthQu = findMonth(customerManagerLevels,managerQuotaSettings,2);
        return new ResultTo().setData("week",findMonth(customerManagerLevels,managerQuotaSettings,1))
                .setData("month",findMonth(customerManagerLevels,managerQuotaSettings,2))
                .setData("year",findMonth(customerManagerLevels,managerQuotaSettings,3));
    }

    private List<ManagerQuotaSettingExt> findMonth(List<JnCustomerManagerLevel> customerManagerLevels, List<JnManagerQuotaSetting> managerQuotaSettings, int i) {
        return customerManagerLevels.stream().map(managerLevel -> find(managerLevel,managerQuotaSettings,i)).collect(Collectors.toList());
    }

    private ManagerQuotaSettingExt find(JnCustomerManagerLevel managerLevel,List<JnManagerQuotaSetting> managerQuotaSettings,int i){
        JnManagerQuotaSetting  quotaSetting = managerQuotaSettings.stream().filter(managerQuotaSetting -> managerQuotaSetting.getLevelId().equals(managerLevel.getLevelId()) && managerQuotaSetting.getPeriod() == i)
                .findFirst().orElse(null);
        ManagerQuotaSettingExt quotaSettingExt = DozerUtil.map(quotaSetting,ManagerQuotaSettingExt.class);
        quotaSettingExt.setLevelName(managerLevel.getLevelName());
        return quotaSettingExt ;
    }


    private void selectExts(ManagerLevelExt mLevelExt) {
        mLevelExt.setmRisk(managerRiskService.selectOne(new JnManagerRisk().setLevelId(mLevelExt.getLevelId())));
        mLevelExt.setmMonthAssess(managerMonthAssessService.selectOne(new JnManagerMonthAssess().setLevelId(mLevelExt.getLevelId())));
        mLevelExt.setmQuotaSettings(managerQuotaSettingService.select(new JnManagerQuotaSetting().setLevelId(mLevelExt.getLevelId())));
        mLevelExt.setmLevelParam(managerLevelParamService.selectOne(new JnManagerLevelParam().setLevelId(mLevelExt.getLevelId())));
        mLevelExt.setmPerformanceParam(managerPerformanceParamService.selectOne(new JnManagerPerformanceParam().setLevelId(mLevelExt.getLevelId())));
    }


    @PostMapping("/saveUpdateLevel")
    public ResultTo saveUpdateLevel(JnCustomerManagerLevel managerLevel){
        jnCustomerManagerLevelService.saveUpdate(managerLevel);
        return new ResultTo();
    }



    @PostMapping("/saveUpdateRisk")
    public ResultTo saveUpdateRisk(String managerRiskJson){
        managerRiskJson = HtmlUtils.htmlUnescape(managerRiskJson);
        Gson gson = GsonUtil.build();
        Type type = new TypeToken<List<JnManagerRisk>>(){}.getType();
        List<JnManagerRisk> managerRisks = gson.fromJson(managerRiskJson,type);
        managerRiskService.saveUpdate(managerRisks);
        return new ResultTo();
    }

    @PostMapping("/saveUpdateMonthAssess")
    public ResultTo saveUpdateMonthAssess(String monthAssessJson){
        monthAssessJson = HtmlUtils.htmlUnescape(monthAssessJson);
        Gson gson = GsonUtil.build();
        Type type = new TypeToken<List<JnManagerMonthAssess>>(){}.getType();
        List<JnManagerMonthAssess> managerMonthAssesses = gson.fromJson(monthAssessJson,type);
        managerMonthAssessService.saveUpdate(managerMonthAssesses);
        return new ResultTo();
    }

    @PostMapping("/saveUpdateManagerQuotaSetting")
    public ResultTo saveUpdateManagerQuotaSetting(String quotaSettingJson){
        quotaSettingJson = HtmlUtils.htmlUnescape(quotaSettingJson);
        Gson gson = GsonUtil.build();
        Type type = new TypeToken<List<JnManagerQuotaSetting>>(){}.getType();
        List<JnManagerQuotaSetting> managerQuotaSettings = gson.fromJson(quotaSettingJson,type);
        managerQuotaSettingService.saveUpdate(managerQuotaSettings);
        return new ResultTo();
    }

    @PostMapping("/saveUpdateManagerLevelParam")
    public ResultTo saveUpdateManagerLevelParam(String managerLevelParamJson){
        managerLevelParamJson = HtmlUtils.htmlUnescape(managerLevelParamJson);
        Gson gson = GsonUtil.build();
        Type type = new TypeToken<List<JnManagerLevelParam>>(){}.getType();
        List<JnManagerLevelParam> managerLevelParams = gson.fromJson(managerLevelParamJson,type);
        managerLevelParamService.saveUpdate(managerLevelParams);
        return new ResultTo();
    }


    @PostMapping("/saveUpdateManagerPerformanceParam")
    public ResultTo saveUpdateManagerPerformanceParam(String managerPerformParamJson){
        managerPerformParamJson = HtmlUtils.htmlUnescape(managerPerformParamJson);
        Gson gson = GsonUtil.build();
        Type type = new TypeToken<List<JnManagerPerformanceParam>>(){}.getType();
        List<JnManagerPerformanceParam> managerPerformanceParams = gson.fromJson(managerPerformParamJson,type);
        managerPerformanceParamService.saveUpdate(managerPerformanceParams);
        return new ResultTo();
    }



}
